#!/bin/sh
################################################################################
# Xinc - Continuous Integration for PHP.                                         
# 
# package Xinc
# author David Ellis
# author Gavin Foster
# author Arno Schneider
# author Alexander Opitz
# version 2.3
# copyright 2007 David Ellis, One Degree Square
# license  http://www.gnu.org/copyleft/lgpl.html GNU/LGPL, see license.php
# 	This file is part of Xinc.
# 	Xinc is free software; you can redistribute it and/or modify
# 	it under the terms of the GNU Lesser General Public License as published by
# 	the Free Software Foundation; either version 2.1 of the License, or
# 	(at your option) any later version.
# 
# 	Xinc is distributed in the hope that it will be useful,
# 	but WITHOUT ANY WARRANTY; without even the implied warranty of
# 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# 	GNU Lesser General Public License for more details.
# 
# 	You should have received a copy of the GNU Lesser General Public License
# 	along with Xinc, write to the Free Software
# 	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
################################################################################

### BEGIN CHKCONFIG INFO ###
# chkconfig:   2345 86 14
# description: Xinc - Continous Integration Server
# pidfile:     /var/log/xinc.pid
### END CHKCONFIG INFO ###
### BEGIN INIT INFO
# Provides:          xinc
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop xinc continous integration server
### END INIT INFO

NAME=xinc
DESC="Xinc - Continuous Integration JobControl Server"

PIDFILE=/var/run/$NAME.pid
DAEMON=/usr/bin/$NAME

CONFIG=/etc/$NAME/system.xml
PROJECTS=/etc/$NAME/conf.d/*.xml
LOG=/var/log/$NAME.log
STATUS=/var/xinc/status
DATADIR=/var/xinc/projects
SHUTDOWN_FILE=/var/xinc/status/.shutdown

# send log output to text-log-file
# logfile is going to be used for build-based logging only
OPTIONS="-f $CONFIG -p $DATADIR -s $STATUS -w $DATADIR -l $LOG $PROJECTS"

# default daemon behaviour if no helper scripts are defined
start_daemon=""
start_daemon_end="&"
start_daemon_options=""

stop_daemon="killall $NAME;"
stop_daemon_end="rm $PIDFILE"
stop_daemon_options=""

logbegin="echo -n"
logend=`printf "echo .\n"`


restart_daemon="touch $SHUTDOWN_FILE";
restart_daemon_wait="sleep 1s"

if [ ! -r "$STATUS" ]; then
    mkdir $STATUS
fi

if [ -r /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
    logbegin="log_begin_msg"
    logend="log_end_msg"

    start_daemon="start_daemon"
    start_daemon_options="--pidfile $PIDFILE"
    start_daemon_end="&"

    stop_daemon="killproc $DAEMON"
    stop_daemon_options=""
    stop_daemon_end=""
fi;

if [ -r /etc/init.d/functions ]; then
    . /etc/init.d/functions
    logbegin="echo -n"
    logend="echo"

    start_daemon="daemon"
    start_daemon_options="--pidfile $PIDFILE"
    start_daemon_end="&"

    stop_daemon="killproc -p $PIDFILE"
    stop_daemon_options=""
    stop_daemon_end=""
fi;

if [ -r /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
    logbegin="echo -n"
    logend="echo"

    start_daemon="daemon"
    start_daemon_options=""
    start_daemon_end="&"
    
    stop_daemon="killproc $DAEMON"
    stop_daemon_options=""
    stop_daemon_end=""

    OPTIONS="--pid-file=/var/run/$NAME.pid $OPTIONS"
fi;

if [ -x /sbin/start-stop-daemon ]; then
    start_daemon="start-stop-daemon"
    DAEMON="$DAEMON --"
    start_daemon_options=" --pidfile $PIDFILE --start --background --exec" 
    start_daemon_end=""

    stop_daemon="start-stop-daemon"
    stop_daemon_options="--pidfile $PIDFILE --stop --quiet --name $NAME"
    stop_daemon_end=""
fi

restart_xinc() {

    if [ $1 ]; then
        sleep $1
    fi

    $logbegin "Restarting $DESC"
    $restart_daemon
    $restart_daemon_wait
    if [ -r $PIDFILE ]; then
        read pid < "$PIDFILE"
        ps -p "$pid" > /dev/null 2>&1
        retval=$?
        while [ $retval -eq 0 ]; do
            $restart_daemon_wait
            echo -n "."
            ps -p "$pid" > /dev/null 2>&1
            retval=$?
        done
    fi
    $logend
    start_xinc

}

start_xinc() {
    $logbegin "Starting $DESC"

    if [ -r $PIDFILE ]; then
      read pid < "$PIDFILE"
      TEST=`ps -o pid --no-heading -p $pid`
      if [ "${pid}" -eq "${TEST}" ]; then
        
        $logbegin ":Xinc already running"
        $logend
        exit 0
      else 
        $logbegin ":PID File $PIDFILE exists but Xinc is not running"
        $logend
      fi
    fi 
    $start_daemon $start_daemon_options $DAEMON $OPTIONS $start_daemon_end
    $logend

}

stop_xinc() {
    $logbegin "Stopping $DESC"
    $restart_daemon
    $restart_daemon_wait
    if [ -r $PIDFILE ]; then
        read pid < "$PIDFILE"
        ps -p "$pid" > /dev/null 2>&1
        retval=$?
        while [ $retval -eq 0 ]; do
            $restart_daemon_wait
            echo -n "."
            ps -p "$pid" > /dev/null 2>&1
            retval=$?
        done
    fi
    $logend
    if [ -f $SHUTDOWN_FILE ]; then
        rm -f $SHUTDOWN_FILE
    fi;

}

case "$1" in
    start)
        start_xinc
    ;;
    stop)
        stop_xinc
    ;;
    restart|force-reload)
        restart_xinc $2
    ;;
    status)
        echo -n "Status of $DESC: "
        if [ ! -r "$PIDFILE" ]; then
            echo "$NAME is not running."
            exit 3
        fi
        read pid < "$PIDFILE"
        ps -p "$pid" > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo "$NAME is running."
            exit 0
        else
            echo "$NAME is not running but $PIDFILE exists."
            exit 1
        fi
    ;;
    *)
        N=/etc/init.d/${0##*/}
        echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
        exit 1
    ;;
esac

exit 0
